package com.ittable.datasource.route.dao;

import org.mybatis.spring.SqlSessionTemplate;

/**
 * @author srh
 * @date 2020/12/20
 **/
public class MybatisBaseDao {

    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    public void init() {
        if (sqlSessionTemplate == null || sqlSessionTemplate.getSqlSessionFactory() == null) {
            throw new RuntimeException("数据库映射文件必须指定");
        }
    }

    public Object executeQueryForObject(String statementName, Object parameterObject) {
        return sqlSessionTemplate.selectOne(statementName, parameterObject);
    }

    public int executeInsert(String statementName, Object parameterObject) {
        return sqlSessionTemplate.insert(statementName, parameterObject);
    }
}
